package com.utils;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

public class STAXUtil {

    /**
     * 调用欧洲ERP接口，根据返回报文xml和接口名称interfaceName解析返回报文
     * @param xml
     * @param interfaceName
     * @return
     * @throws DocumentException
     */
    public static List<Map<String, Object>> getEuropMapList(String xml, String interfaceName) throws DocumentException {
        List<Map<String, Object>> maps = new ArrayList<>();
        InputSource in = new InputSource(new StringReader(xml));
        in.setEncoding("UTF-8");
        SAXReader reader = new SAXReader();
        Document document = reader.read(in);// 如果是xml文件这里直接写xml文件的绝对路径
        Element rootElt = document.getRootElement();// 获取根节点
        Element element = rootElt.element("Body");
        Element element1 = element.element(interfaceName + "Response");
        Element element2 = element1.element(interfaceName + "Result");
        List<Element> elements = element2.elements();
        List<Element> list = elements.get(0).elements();
        for (Element e : list) {
            maps.add(xmltoMap(e));
        }
        return maps;
    }

    /**
     * xml转成map
     * @param
     * @return
     */
    public static Map<String, Object> xmltoMap(Element rootElement) {
        Map<String, Object> mapXml = new HashMap<String, Object>();
        elementToMap(mapXml, rootElement);
        return mapXml;
    }

    /**
     * 遍历子节点
     * @param map
     * @param rootElement
     */
    public static void elementToMap(Map<String, Object> map, Element rootElement) {
        //获得当前节点的子节点
        List<Element> elements = rootElement.elements();
        Map<String, Object> childMap = new HashMap<String, Object>();
        //如果还存在子节点，就考虑list情况，继续递归
        for (Element element : elements) {
            List<Element> es = element.elements();
            if (es.size() > 0) {
                //获取当前节点下的子节点
                ArrayList<Map> list = new ArrayList<Map>();
                for (Element e : es) {
                    elementChildToList(list, e);
                }
                map.put(element.getName(), list);
            } else {
                map.put(element.getName(), element.getText());
            }
        }
    }

    /**
     * 递归子节点
     * @param arrayList
     * @param rootElement
     */
    public static void elementChildToList(ArrayList<Map> arrayList, Element rootElement) {
        //获得当前节点的子节点
        List<Element> elements = rootElement.elements();
        if (elements.size() > 0) {
            ArrayList<Map> list = new ArrayList<Map>();
            Map<String, Object> sameTempMap = new HashMap<String, Object>();
            for (Element element : elements) {
                elementChildToList(list, element);
                sameTempMap.put(element.getName(), element.getText());
            }
            arrayList.add(sameTempMap);
        }
    }

    /**
     * 调用欧洲ERP接口，根据返回报文Result
     * @param xml
     * @param interfaceName
     * @return
     * @throws DocumentException
     */
    public static String getEuropResultStr(String xml, String interfaceName) throws DocumentException {
        InputSource in = new InputSource(new StringReader(xml));
        in.setEncoding("UTF-8");
        SAXReader reader = new SAXReader();
        Document document = reader.read(in);// 如果是xml文件这里直接写xml文件的绝对路径
        Element rootElt = document.getRootElement();// 获取根节点
        Element element = rootElt.element("Body");
        Element element1 = element.element(interfaceName + "Response");
        Element element2 = element1.element(interfaceName + "Result");
        Element element3 = element2.element("Response");
        Element element4 = element3.element("Result");
        return element4.getText();
    }
}
